﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using KQReport.Utils;
using System.Linq;
using Utils.DB.OleDb;

namespace KQReport
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += ApplicationThreadException;
            //DevExpress.Skins.SkinManager.EnableFormSkins();
            #region Set

            if (string.IsNullOrEmpty(Conf.Cn.Value))
            {
                string[] paths =
                {
                    @"C:\Program Files\Att2008\att2000.mdb",
                    @"C:\Program Files (x86)\Att2008\att2000.mdb",
                    @"C:\Att2008\att2000.mdb",
                    @"D:\Program Files\Att2008\att2000.mdb",
                    @"D:\Program Files (x86)\Att2008\att2000.mdb",
                    @"D:\Att2008\att2000.mdb",
                    @"E:\Program Files\Att2008\att2000.mdb",
                    @"E:\Program Files (x86)\Att2008\att2000.mdb",
                    @"E:\Att2008\att2000.mdb"
                };
                var path = paths.FirstOrDefault(System.IO.File.Exists);
                if (string.IsNullOrEmpty(path))
                {
                    var dialog = new OpenFileDialog
                    {
                        FileName = paths[0],
                        Filter = @"*.mdb|*.mdb",
                        Multiselect = false
                    };
                    var result = dialog.ShowDialog();
                    if (result != DialogResult.Yes)
                        return;
                    path = dialog.FileName;
                }
                Conf.Cn.Value = path;
            }
            SqlHelperEx.Connection = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}'", Conf.Cn.Value);

            #endregion

            Application.Run(new FmMain());
        }

        #region Error

        private static void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
        {
            try
            {
                XtraMessageBox.Show(String.Format("系统出现错误\r\n{0}", e.Exception.Message), "错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            catch (Exception exception)
            {
                var error = exception.ToString();
                XtraMessageBox.Show(String.Format("系统出现错误\r\n{0}", error), "错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}